package com.surprise.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.surprise.common.bean.Assist;
import com.surprise.common.bean.Result;
import com.surprise.common.util.StringUtils;
import com.surprise.dao.SpUserDao;
import com.surprise.dao.SpUserTokenDao;
import com.surprise.pojo.SpUser;
import com.surprise.pojo.SpUserToken;
import com.surprise.service.SpUserService;

/**
 * SpUser的service接口的实现类
 * 
 * @author
 *
 */
@Service
public class SpUserServiceImpl implements SpUserService {

	@Autowired
	private SpUserDao spUserDao;
	@Autowired
	private SpUserTokenDao spUserTokenDao;

	@Override
	public long getSpUserRowCount(Assist assist) {
		return spUserDao.getSpUserRowCount(assist);
	}

	@Override
	public List<SpUser> selectSpUser(Assist assist) {
		return spUserDao.selectSpUser(assist);
	}

	@Override
	public SpUser selectSpUserById(Integer id) {
		return spUserDao.selectSpUserById(id);
	}

	@Override
	public SpUser selectSpUserSingleByObj(SpUser obj) {
		return spUserDao.selectSpUserSingleByObj(obj);
	}

	@Override
	public List<SpUser> selectSpUserListByObj(SpUser obj) {
		return spUserDao.selectSpUserListByObj(obj);
	}

	@Override
	public int insertSpUser(SpUser value) {
		return spUserDao.insertSpUser(value);
	}

	@Override
	public int insertNotNullSpUser(SpUser value) {
		return spUserDao.insertNotNullSpUser(value);
	}

	@Override
	public int insertSpUserByBatch(List<SpUser> value) {
		return spUserDao.insertSpUserByBatch(value);
	}

	@Override
	public int deleteSpUserById(Integer id) {
		return spUserDao.deleteSpUserById(id);
	}

	@Override
	public int deleteSpUserByAssist(Assist assist) {
		return spUserDao.deleteSpUserByAssist(assist);
	}

	@Override
	public int updateSpUserById(SpUser value) {
		return spUserDao.updateSpUserById(value);
	}

	@Override
	public int updateNotNullSpUserById(SpUser value) {
		return spUserDao.updateNotNullSpUserById(value);
	}

	@Override
	public int updateSpUser(SpUser value, Assist assist) {
		return spUserDao.updateSpUser(value, assist);
	}

	@Override
	public int updateNotNullSpUser(SpUser value, Assist assist) {
		return spUserDao.updateNotNullSpUser(value, assist);
	}

	/**
	 * 用户登陆操作
	 */
	@Override
	public Result getLogin(String phone, String password, String serialCode, int referee, String wechat_openid,
			String qq_openid, String open_code, String device) {

		Result result = new Result(); // 创建结果返回对象,用以返回json数据

		// 判断是手机号码登陆还是第三方登陆
		if(StringUtils.isEmpty(phone) && StringUtils.isEmpty(password)) {
			// 第三方登陆
			
			
			
			
		}else {
			// 手机号及密码方式登陆
			SpUser user = new SpUser();
			user.setPhone(phone);
			user = spUserDao.selectSpUserSingleByObj(user);
			
			password = StringUtils.getMD5(password);
			
			if(user != null) {
				if(password.equals(user.getUserPwd())) {
					if(user.getIsDelete() == 1) {
						result.setFailure(202, "账号已注销, 如需帮助请联系客户!");
					}
					
					if(user.getPhoneUnbound() == 0) {
						Map<String, Object> data = new HashMap<>();
						data.put("u_id", user.getId());
						data.put("u_name", user.getUserName());
						data.put("authkey", createAuthkey(user.getId(), serialCode, user.getUserPwd()));
						result.setSuccess(210, "您的帐号手机已解绑, 为了您的帐号安全请及时重绑!", data);
					}
					
					
					
				}else {
					result.setFailure(204, "密码错误!");
				}
				
				
			}
			
			
		}

		return result;
	}

	/**
	 * 生成token密钥
	 * @param user_id		用户ID
	 * @param serialCode	设备序列号
	 * @param password		密码
	 * @return
	 */
	private String createAuthkey(Integer user_id, String serialCode, String password) {
		
		String token;
		serialCode = StringUtils.isEmpty(serialCode) == true ? "web" : serialCode;
		token = StringUtils.getMD5(user_id + serialCode + System.currentTimeMillis());
		
		SpUserToken userToken = new SpUserToken();
		userToken.setId(user_id);
		userToken.setToken(token);
		userToken.setSerialCode(serialCode);
		
		int result = spUserTokenDao.saveOrUpdateSpUserToken(userToken);	// 保存或更新	未完成	
		
		return token;
	}

}